package Game;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;


@SuppressWarnings("serial")
public abstract class GameCanvas extends Canvas implements Runnable,KeyListener{
 public static final long ONE_SECOND_MILI = 1000;
 protected int frameW;
 protected int frameH;
 protected long fps;
 private long period = 15;
 private BufferStrategy buff;
 private Graphics graph;
 private Color bckGround=(Color.GRAY);
 private Image bckGround_img;
 private Thread t;
 public static boolean left;
 public static boolean right;
 public static boolean up;
 public static boolean down;
 public static int lastpressedx=1;
 public static int newlastpressedx;
 public static int lastpressedy=2;
 public static int newlastpressedy;
 public static int lastpressedxy=2;
 public static boolean spaceBar;

 private String drawFps = "0";
 	public GameCanvas(int w,int h){
 		this.frameW=w;
 		this.frameH=h;
		this.setIgnoreRepaint(true);
		this.setBounds(0,0,frameW,frameH);
		this.setBackground(Color.GREEN);
		this.setVisible(true);
 	}
 	
 	public GameCanvas(int w,int h,Color bck){
 		this.frameW=w;
 		this.frameH=h;
 		this.bckGround=bck;
		this.setIgnoreRepaint(true);
		this.setBounds(0,0,frameW,frameH);
		this.setBackground(bckGround);
		this.setVisible(true);
 	}
 	
 	public void addNotify(){
		super.addNotify();
		this.createBufferStrategy(2);
		this.buff=this.getBufferStrategy();
		requestFocus();
		startGame();
	}
 	public void startGame(){
		if (t==null){
			t=new Thread(this);
			t.start();
		}
	}
	public void run(){
		while(true){
			long beginTime=System.currentTimeMillis();
			Update();
			Render();
			Draw();
			fps=System.currentTimeMillis() - beginTime ;
			long sleepTime=period-fps;
			if (sleepTime == 30) sleepTime = -1;
			fps= ONE_SECOND_MILI / ((period * 2) - sleepTime);
			try{
				if (sleepTime > 0){
					Thread.sleep(sleepTime);
					
				}
			}
			catch(Exception e){
			}
		}
	}
	public void Render(){
		graph = buff.getDrawGraphics();
		
		if (!HasImgBackground()){
			graph.setColor(bckGround);
			graph.fillRect(0, 0, frameW, frameH);
		}else{
			graph.drawImage(bckGround_img, 0, 0, frameW, frameH,null);
		}
		graph.setColor(new Color(255,255,255));
		graph.drawString("FPS: " + fps , 10, 15);
		Paint(graph);
	}
	private void Draw(){
		if(!buff.contentsLost()){
			buff.show();
			if(graph != null){
				graph.dispose();
			}
		}
	}
	private boolean HasImgBackground(){
		if (bckGround_img==null){
			return false;
		}
		return true;
	}
	public void setBackgroundImg(Image image){
		this.bckGround_img=image;
	}
	public void deleteBackground(){
		this.bckGround_img=null;
	}
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_CONTROL){
			spaceBar=true;
		}
		if(e.getKeyCode()==KeyEvent.VK_LEFT){
			left=true;
			if(lastpressedx!=1)
				newlastpressedx=lastpressedx;
			lastpressedx=1;
			lastpressedxy=1;
		}
		if(e.getKeyCode()==KeyEvent.VK_UP){
			up=true;
			if(lastpressedy!=1)
				newlastpressedy=lastpressedy;
			lastpressedy=1;
			lastpressedxy=2;
		}
		if(e.getKeyCode()==KeyEvent.VK_RIGHT){
			right=true;
			if(lastpressedx!=2)
				newlastpressedx=lastpressedx;
			lastpressedx=2;
			lastpressedxy=1;
		}
		if(e.getKeyCode()==KeyEvent.VK_DOWN){
			down=true;
			if(lastpressedy!=2)
				newlastpressedy=lastpressedy;
			lastpressedy=2;
			lastpressedxy=2;
		}
		if(e.getKeyCode()==KeyEvent.VK_C){
			Mainth.level.Level().movem.createbat();
		}
	}
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_CONTROL){
			spaceBar=false;
		}
		if(e.getKeyCode()==KeyEvent.VK_LEFT){
			left=false;
			if(right)
				if(newlastpressedx!=0)
				lastpressedx=newlastpressedx;
			if(!right){
				if(up||down){
					lastpressedxy=2;
				}
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_UP){
			up=false;
			if(down)
				if(newlastpressedy!=0)
				lastpressedy=newlastpressedy;
			if(!down){
				if(left||right){
					lastpressedxy=1;
				}
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_RIGHT){
			right=false;
			if(left)
				if(newlastpressedx!=0)
				lastpressedx=newlastpressedx;
			if(!left){
				if(up||down){
					lastpressedxy=2;
				}
			}
		}
		if(e.getKeyCode()==KeyEvent.VK_DOWN){
			down=false;
			if(up)
				if(newlastpressedy!=0)
				lastpressedy=newlastpressedy;
			if(!up){
				if(left||right){
					lastpressedxy=1;
				}
			}
		}
	}
	public void keyTyped(KeyEvent e) {
	}
	abstract void Update();
	abstract void Paint(Graphics g);
}
